<?php
/**
 * PHP Class
 *
 * LICENSE
 *
 * Processing: translate
 *
 * @category   	Joomzf Cms
 * @package    	Translate
 * @copyright  	Copyright vinanewstar.vn@gmail.com
 * @author		GiangNam (vinanewstar.vn@gmail.com)
 * @license    	GNU General Public License Version 2 or later (the "GPL")
 * @version     v1.0 May 27, 2012 - 4:54:31 PM
 */
class Joomzf_Translate{
	
	public  $_data;
	public $config;
	public $locale;
	public $template;
	public $component;
	
	public $language;
	
	public $area;
	
	public function __construct(Joomzf_Controller_Action $action){
		
		$this->area = $action->getArea();
		$this->template = $action->getTemplate();
		$this->locale = $action->getLocale();
		$this->component = $action->getComponent();
		
		$this->language = $this->locale->getLocale();
		
		$data = Joomzf_Cache::getVariableFromCache(Joomzf_Cache::initLanguage(),"languages", $this, "getData");
		
		$options = array('adapter'=>'Array','content'=>$data ,'locale'=>$this->language);
		$trans = new Zend_Translate($options);
		
		Zend_Registry::set("Zend_Translate", $trans);
	}
	/**
	 * @return the languages (array)
	 */
	public function getData(){
		$this->_loadTemplateLanguage();
		$this->_loadComponentLanguage();
		$this->_loadModuleLanguages();
		return $this->_data;
	}
	public function addFile($fileConfigs = null){
		if(file_exists($fileConfigs)){
			$config = new Joomzf_Config($fileConfigs,$this->language,'xml');
			$word = $config->getConfig('word');
			
			if(count($word)>0)foreach ($word as $value){
				$this->_data[$value['alias']] = $value['value'];
			}
		}
	} 
	/**
	 * load template language
	 */
	final private function _loadTemplateLanguage(){
		$languageFilePath = $this->template->getPath() .DS.'languages'.DS.$this->language.DS.$this->language.'.xml'; 
		$this->addFile($languageFilePath);
	}
	/**
	 * load component language
	 */
	final private function _loadComponentLanguage(){
		$comFilePath = $this->component->getPath() .DS.'languages'.DS.$this->language.DS.$this->language.'.xml';
		$this->addFile($comFilePath);
	}
	/**
	 * load all language file all module
	 */
	final private function _loadModuleLanguages(){
		/**
		 * select module list
		 */
		$db = Zend_Registry::get("DBO");
		$sql = $db ->select()
					->from(TBL_PRF.'modules',array('name','area'))
					->where('published = 1');
		$module_list = $db->fetchAll($sql);
		/**
		 * add file for each module
		 */
		foreach ($module_list as $module){
			$modulePath = ($module['area'] == 0) ? FRONTEND_MODULES_PATH : BACKEND_MODULES_PATH;
			$fileConfigs = $modulePath.DS.$module['name'].DS.'languages'.DS.$this->language.DS.$this->language.'.xml';
			$this->addFile($fileConfigs);
		}
	}
	
}